package rad.framework.mda.jpagen.actions;

import rad.framework.mda.generator.Console;
import rad.framework.mda.generator.util.StereotypeHelper;
import rad.framework.mda.util.ModelUtil;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Stereotype;

/**
 * @author boudyacho
 * 
 */
public class AddTableActionExtension extends BaseUtiltiyActionExtension {

	protected static final String TABLE = "javax.persistence.Table";

	public void fillContextMenu(IMenuManager aMenu) {
		getSubMenu(aMenu).add(new AddTableAction());
	}

	protected void execute(Class clazz) {
		if (StereotypeHelper
				.isStereotypeApplied(clazz, StereotypeHelper.ENTITY)) {

			applyTable(clazz);
			Console.getStream().println(
					"Added " + TABLE + " - " + clazz.getQualifiedName());
		}
	}

	protected void applyTable(Class element) {
		Stereotype stereotype = (Stereotype) ModelUtil.getProfileElement(
				element, TABLE);
		if (stereotype != null) {
			if (!ModelUtil.isStereotypeApplied(element, TABLE)) {
				element.applyStereotype(stereotype);
			}
			// TODO Limits 26 characters for columns.
			String name = NameUtil.getAbbreviation(element);
			// + "S"); //TODO do we want this?
			checkLength(element, name, 21);
			element.setValue(stereotype, "name", name);
		}
	}

	private class AddTableAction extends BaseUtilityAction {
		private static final String LABEL = "Add Table Stereotypes";

		public AddTableAction() {
			super(LABEL);
		}
	}
}
